/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package application.tools;

import application.model.DistanceCalcSettings;
import application.model.EntityAccomplishmentReport;
import application.model.EntitySearchResults;
import application.model.GraphEntity;
import application.model.GraphEntityAccomps;
import application.model.GraphEntityKeywords;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 *
 * @author aa6345
 */
public class FormatTools {    

    //EntityID, GraphEntity
    HashMap<Long, GraphEntity> searchResults2ge = new HashMap<Long, GraphEntity>();
    //EntityID, GraphEntityKeywords
    HashMap<Long, GraphEntityKeywords> searchResults2geks = new HashMap<Long, GraphEntityKeywords>();
    //EntityID, GraphEntityAccomps
    HashMap<Long, GraphEntityAccomps> searchResults2geas = new HashMap<Long, GraphEntityAccomps>();
    //distance / weight calculator
    DistanceCalcTools dct = new DistanceCalcTools();

    public HashMap<Long, GraphEntityAccomps> getSearchResults2geas() {
        return searchResults2geas;
    }

    public void setSearchResults2geas(HashMap<Long, GraphEntityAccomps> searchResults2geas) {
        this.searchResults2geas = searchResults2geas;
    }

    public HashMap<Long, GraphEntityKeywords> getSearchResults2geks() {
        return searchResults2geks;
    }

    public void setSearchResults2geks(HashMap<Long, GraphEntityKeywords> searchResults2geks) {
        this.searchResults2geks = searchResults2geks;
    }

    public HashMap<Long, GraphEntity> getSearchResults2ge() {
        return this.searchResults2ge;
    }

    public void setSearchResults2ge(HashMap<Long, GraphEntity> searchResults2graphEntity) {
        this.searchResults2ge = searchResults2graphEntity;
    }

    /*NOT USED ANYMORE*/
    public void entSearchRes2EntObjList(List<EntitySearchResults> entitySearchResults, String searchTerm) {
        double matchWeight = 0.00;
        ArrayList<Long> entityIDList4NumOfMatchesScore = new ArrayList<Long>();

        for(EntitySearchResults entSearchRes : entitySearchResults) {
            //strat calculating distance weight
            matchWeight = dct.calcWeightTypeDegree(entSearchRes, searchTerm, "");

            //add only one instance of entityID
            if(!entityIDList4NumOfMatchesScore.contains(entSearchRes.getEntityID())) {
                entityIDList4NumOfMatchesScore.add(entSearchRes.getEntityID());
            }

            if(this.getSearchResults2ge().containsKey(entSearchRes.getEntityID())) {
                //add accomplishment weight to total weight
                this.getSearchResults2ge().get(entSearchRes.getEntityID()).setTotalWeight(matchWeight + this.getSearchResults2ge().get(entSearchRes.getEntityID()).getTotalWeight());
                //System.out.println("!!!!! Id found in GraphEntity: "+entSearchRes.getEntityID());
                if(this.getSearchResults2geks().containsKey(entSearchRes.getEntityID())) {
                    //System.out.println("!!!!! Id found in GraphEntityKeywords: "+entSearchRes.getEntityID());
                    //check if current searchTerm exists
                    if(this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().containsKey(searchTerm)) {
                        //add entityAccomplishmentID to current list for existing keyword
                        /*
                        System.out.println("MMMMMMMM"+this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().containsKey(searchTerm));
                        System.out.println("MMMMMMMM"+entSearchRes.getEntityAccomplishmentID());
                        System.out.println("MMMMMMMM"+searchTerm);
                        System.out.println("MMMMMMMM"+this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().toString());
*/

                        this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().get(searchTerm).add(entSearchRes.getEntityAccomplishmentID());
                    } else {
                        //add entityAccomplishmentID to a new list for new keyword
                        ArrayList<Long> listOfEntIDs = new ArrayList<Long>();
                        listOfEntIDs.add(entSearchRes.getEntityAccomplishmentID());
                        this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().put(searchTerm, listOfEntIDs);
                    }
                } else {
                    System.out.println("!!!!! Error, Id NOT found in GEKS but exists in GE: "+entSearchRes.getEntityID());
                }
                
                if(this.getSearchResults2geas().containsKey(entSearchRes.getEntityID())) {
                    //System.out.println("!!!!! Id found in GraphEntityAccomplishments: "+entSearchRes.getEntityID());
                    //check if current entityAccomplishment exists
                    if(this.getSearchResults2geas().get(entSearchRes.getEntityID()).getGea().containsKey(entSearchRes.getEntityAccomplishmentID())) {
                        //DO Nothing in entityAccomplishment details to current list for existing accomplishments
                        
                    } else {
                        //add entityAccomplishment details with id as key

                        EntityAccomplishmentReport ear = new EntityAccomplishmentReport();

                        ear.setAccompID(entSearchRes.getEntityAccomplishmentID());
                        ear.setAccompTitle(entSearchRes.getAccompTitle());
                        ear.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                        //ear.setAccomplishmentWeight(matchWeight);
                        //ear.setAccomplishmentWeight(entSearchRes.getAccompTypeWeight());

                        this.getSearchResults2geas().get(entSearchRes.getEntityID()).getGea().put(entSearchRes.getEntityAccomplishmentID(), ear);
                    }

                } else {
                    System.out.println("!!!!! Error, Id NOT found in GEAS but exists in GE: "+entSearchRes.getEntityID());
                }

            } else {
                //create new GraphEntity
                GraphEntity ge = new GraphEntity();
                ge.setEntityActive(entSearchRes.getEntityActive());
                ge.setEntityEmail(entSearchRes.getEntityEmail());
                ge.setEntityTypeDescription(entSearchRes.getEntityTypeDescription());
                ge.setEntityFName(entSearchRes.getEntityFname());
                ge.setEntityMName(entSearchRes.getEntityMname());
                ge.setEntitySName(entSearchRes.getEntitySname());
                ge.setTotalWeight(matchWeight);
                ge.setEntityContactInfo("");
                //add GraphEntity to the main list of entity details
                this.searchResults2ge.put(entSearchRes.getEntityID(), ge);
                //System.out.println("!!!!! Id NOT found in GraphEntity therefore Added: "+entSearchRes.getEntityID());

                //create new keyword entry if required
                if(this.getSearchResults2geks().containsKey(entSearchRes.getEntityID())) {
                    System.out.println("!!!!! Error, Id found in GEKS but Not in GE: "+entSearchRes.getEntityID());
                } else {
                    GraphEntityKeywords geks = new GraphEntityKeywords();
                    ArrayList<Long> entAccompIdList = new ArrayList<Long>();
                    HashMap<String,ArrayList<Long>> kwrdList = new HashMap<String,ArrayList<Long>>();
                    entAccompIdList.add(entSearchRes.getEntityAccomplishmentID());
                    kwrdList.put(searchTerm, entAccompIdList);
                    geks.setGek(kwrdList);
                    this.searchResults2geks.put(entSearchRes.getEntityID(),geks);
                    //System.out.println("!!!!! Id NOT found in GraphEntityKeywords therefore Added: "+entSearchRes.getEntityID());
                }

                //create new acommplishment entry if required
                if(this.getSearchResults2geas().containsKey(entSearchRes.getEntityID())) {
                    System.out.println("!!!!! Error, Id found in GEAS but Not in GE: "+entSearchRes.getEntityID());
                } else {
                    GraphEntityAccomps geas = new GraphEntityAccomps();
                    EntityAccomplishmentReport ear = new EntityAccomplishmentReport();

                    ear.setAccompID(entSearchRes.getEntityAccomplishmentID());
                    ear.setAccompTitle(entSearchRes.getAccompTitle());
                    ear.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                    //ear.setAccomplishmentWeight(entSearchRes.getAccompTypeWeight());

                    geas.getGea().put(entSearchRes.getEntityAccomplishmentID(), ear);

                    this.searchResults2geas.put(entSearchRes.getEntityID(), geas);
                    //System.out.println("!!!!! Id NOT found in GraphEntityAccomps therefore Added: "+entSearchRes.getEntityID());
                }
                
            }

        }




        //HashMap<String,ArrayList<EntitySearchResults>> entityBasedResultsList = new HashMap<String,ArrayList<EntitySearchResults>>();
/*
        for(EntitySearchResults entSearchRes : entitySearchResults) {
            if(this.getSearchResults2graphEntity().containsKey(entSearchRes.getEntityID().toString())) {
                ArrayList<String> listOfEntAccompIDs = new ArrayList<String>();
                HashMap<String, ArrayList<String>> entityKeywordList = new HashMap<String, ArrayList<String>>();
                EntityAccomplishmentReport ea = new EntityAccomplishmentReport();

                System.out.println("vvvvvvvvvvvv"+this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString()));
                System.out.println("vvvvvvvvvvvv"+this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString()).getEntityKeywordList().toString());
                System.out.println("vvvvvvvvvvvv"+searchTerm.toLowerCase()+" -vv- "
                        +this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString()).getEntityKeywordList().get(searchTerm.toLowerCase()));
                System.out.println("vvvvvvvvvvvv"+entSearchRes.getEntityAccomplishmentID().toString());
                
                //matched keywords
                //if(this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString()).getEntityKeywordList().containsKey(entSearchRes.getEntityAccomplishmentID().toString())) {
                if(this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString()).getEntityKeywordList().get(searchTerm.toLowerCase()).contains(entSearchRes.getEntityAccomplishmentID().toString())) {

                    //add to an existing matched keyword list
                    this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString())
                            .getEntityKeywordList().get(searchTerm.toLowerCase())
                            .add(entSearchRes.getEntityAccomplishmentID().toString());
                } else {
                    //create matched keyword list
                    listOfEntAccompIDs.add(entSearchRes.getEntityAccomplishmentID().toString());
                    entityKeywordList.put(searchTerm.toLowerCase(), listOfEntAccompIDs);
                    this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString())
                            .setEntityKeywordList(entityKeywordList);
                }

                //create entity accomplishment details               
                ea.setAccompTitle(entSearchRes.getAccompTitle());
                ea.setAccompID(entSearchRes.getEntityAccomplishmentID());
                ea.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                ea.setAccomplishmentWeight(entSearchRes.getAccompPriority());

                this.searchResults2graphEntity.get(entSearchRes.getEntityID().toString())
                        .getEntityAccompList().put(entSearchRes.getEntityAccomplishmentID().toString(), ea);


            } else {
                GraphEntity ge = new GraphEntity();
                EntityAccomplishmentReport ea = new EntityAccomplishmentReport();
                HashMap<String, ArrayList<String>> entityKeywordList = new HashMap<String, ArrayList<String>>();
                ArrayList<String> listOfEntAccompIDs = new ArrayList<String>();

                //create main entity details
                ge.setEntityID(entSearchRes.getEntityID().toString());
                ge.setEntityFName(entSearchRes.getEntityFname());
                ge.setEntityMName(entSearchRes.getEntityMname());
                ge.setEntitySName(entSearchRes.getEntitySname());
                ge.setEntityEmail(entSearchRes.getEntityEmail());
                ge.setEntityActive(entSearchRes.getEntityActive());
                ge.setEntityTypeDescription(entSearchRes.getEntityTypeDescription());

                //create matched keyword list
                listOfEntAccompIDs.add(entSearchRes.getEntityAccomplishmentID().toString());
                entityKeywordList.put(searchTerm.toLowerCase(), listOfEntAccompIDs);
                ge.setEntityKeywordList(entityKeywordList);

                //create entity accomplishment details               
                ea.setAccompTitle(entSearchRes.getAccompTitle());
                ea.setAccompID(entSearchRes.getEntityAccomplishmentID());
                ea.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                ea.setAccomplishmentWeight(entSearchRes.getAccompPriority());

                ge.getEntityAccompList().put(entSearchRes.getEntityAccomplishmentID().toString(), ea);

                this.searchResults2graphEntity.put(entSearchRes.getEntityID().toString(), ge);
                
            }
        }
*/
    }

    public void entSearchRes2EntObjList2(List<EntitySearchResults> entitySearchResults, String searchTerm, String searchOperator, List<DistanceCalcSettings> distanceCalcSettings) {
        double matchWeight = 0.00;
        dct.setDistanceCalcSettingsList(distanceCalcSettings);
        //ArrayList<Long> entityIDList4NumOfMatchesScore = new ArrayList<Long>();
        HashMap<Long,Double> entityIDandScoreList4NumOfMatchesScore = new HashMap<Long,Double>();

        for(EntitySearchResults entSearchRes : entitySearchResults) {
            //strat calculating distance weight
            matchWeight = dct.calcWeightTypeDegree(entSearchRes, searchTerm, searchOperator);
            
            //add only one instance of entityID
            //if(!entityIDList4NumOfMatchesScore.contains(entSearchRes.getEntityID())) {
            if(!entityIDandScoreList4NumOfMatchesScore.containsKey(entSearchRes.getEntityID())) {
                //entityIDList4NumOfMatchesScore.add(entSearchRes.getEntityID());
                entityIDandScoreList4NumOfMatchesScore.put(entSearchRes.getEntityID(), matchWeight);
            } else {
                entityIDandScoreList4NumOfMatchesScore.put(entSearchRes.getEntityID(), entityIDandScoreList4NumOfMatchesScore.get(entSearchRes.getEntityID()) + matchWeight);
            }

            if(this.getSearchResults2ge().containsKey(entSearchRes.getEntityID())) {
                //add accomplishment weight to total weight
                
                this.getSearchResults2ge().get(entSearchRes.getEntityID()).setTotalWeight(matchWeight + this.getSearchResults2ge().get(entSearchRes.getEntityID()).getTotalWeight());
                //System.out.println("!!!!! Id found in GraphEntity: "+entSearchRes.getEntityID());
                if(this.getSearchResults2geks().containsKey(entSearchRes.getEntityID())) {
                    //System.out.println("!!!!! Id found in GraphEntityKeywords: "+entSearchRes.getEntityID());
                    //check if current searchTerm exists
                    if(this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().containsKey(searchTerm)) {
                        //add entityAccomplishmentID to current list for existing keyword
                        
                        this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().get(searchTerm).add(entSearchRes.getEntityAccomplishmentID());
                    } else {
                        //add entityAccomplishmentID to a new list for new keyword
                        ArrayList<Long> listOfEntIDs = new ArrayList<Long>();
                        listOfEntIDs.add(entSearchRes.getEntityAccomplishmentID());
                        this.getSearchResults2geks().get(entSearchRes.getEntityID()).getGek().put(searchTerm, listOfEntIDs);
                    }
                } else {
                    System.out.println("!!!!! Error, Id NOT found in GEKS but exists in GE: "+entSearchRes.getEntityID());
                }

                if(this.getSearchResults2geas().containsKey(entSearchRes.getEntityID())) {
                    //System.out.println("!!!!! Id found in GraphEntityAccomplishments: "+entSearchRes.getEntityID());
                    //check if current entityAccomplishment exists
                    if(this.getSearchResults2geas().get(entSearchRes.getEntityID()).getGea().containsKey(entSearchRes.getEntityAccomplishmentID())) {
                        //DO Nothing in entityAccomplishment details to current list for existing accomplishments

                    } else {
                        //add entityAccomplishment details with id as key

                        EntityAccomplishmentReport ear = new EntityAccomplishmentReport();

                        ear.setAccompID(entSearchRes.getEntityAccomplishmentID());
                        ear.setAccompTitle(entSearchRes.getAccompTitle());
                        ear.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                        //ear.setAccomplishmentWeight(matchWeight);
                        //ear.setAccomplishmentWeight(entSearchRes.getAccompTypeWeight());

                        this.getSearchResults2geas().get(entSearchRes.getEntityID()).getGea().put(entSearchRes.getEntityAccomplishmentID(), ear);
                    }

                } else {
                    System.out.println("!!!!! Error, Id NOT found in GEAS but exists in GE: "+entSearchRes.getEntityID());
                }

            } else {
                //create new GraphEntity
                GraphEntity ge = new GraphEntity();
                ge.setEntityActive(entSearchRes.getEntityActive());
                ge.setEntityEmail(entSearchRes.getEntityEmail());
                ge.setEntityTypeDescription(entSearchRes.getEntityTypeDescription());
                ge.setEntityFName(entSearchRes.getEntityFname());
                ge.setEntityMName(entSearchRes.getEntityMname());
                ge.setEntitySName(entSearchRes.getEntitySname());
                ge.setTotalWeight(matchWeight);
                ge.setEntityContactInfo("");
                //add GraphEntity to the main list of entity details
                this.searchResults2ge.put(entSearchRes.getEntityID(), ge);
                //System.out.println("!!!!! Id NOT found in GraphEntity therefore Added: "+entSearchRes.getEntityID());

                //create new keyword entry if required
                if(this.getSearchResults2geks().containsKey(entSearchRes.getEntityID())) {
                    System.out.println("!!!!! Error, Id found in GEKS but Not in GE: "+entSearchRes.getEntityID());
                } else {
                    GraphEntityKeywords geks = new GraphEntityKeywords();
                    ArrayList<Long> entAccompIdList = new ArrayList<Long>();
                    HashMap<String,ArrayList<Long>> kwrdList = new HashMap<String,ArrayList<Long>>();
                    entAccompIdList.add(entSearchRes.getEntityAccomplishmentID());
                    kwrdList.put(searchTerm, entAccompIdList);
                    geks.setGek(kwrdList);
                    this.searchResults2geks.put(entSearchRes.getEntityID(),geks);
                    //System.out.println("!!!!! Id NOT found in GraphEntityKeywords therefore Added: "+entSearchRes.getEntityID());
                }

                //create new acommplishment entry if required
                if(this.getSearchResults2geas().containsKey(entSearchRes.getEntityID())) {
                    System.out.println("!!!!! Error, Id found in GEAS but Not in GE: "+entSearchRes.getEntityID());
                } else {
                    GraphEntityAccomps geas = new GraphEntityAccomps();
                    EntityAccomplishmentReport ear = new EntityAccomplishmentReport();

                    ear.setAccompID(entSearchRes.getEntityAccomplishmentID());
                    ear.setAccompTitle(entSearchRes.getAccompTitle());
                    ear.setAccomplishmentType(entSearchRes.getAccompTypeDescription());
                    //ear.setAccomplishmentWeight(entSearchRes.getAccompTypeWeight());

                    geas.getGea().put(entSearchRes.getEntityAccomplishmentID(), ear);

                    this.searchResults2geas.put(entSearchRes.getEntityID(), geas);
                    //System.out.println("!!!!! Id NOT found in GraphEntityAccomps therefore Added: "+entSearchRes.getEntityID());
                }

            }
        }

        
        //calculate option 3 in distance calculation algoritham which says:
        //if only one entity matched searched keyword then multiply entity's
        // matchWeight by 8.  If 2 to 4 entities mastched a keyword then multiply
        //by 4.  If keyword generates more than 12.55 of total matches per entity
        //search then multiply by 1 else by2.

        //if(entityIDList4NumOfMatchesScore.size() == 1) {
        if(entityIDandScoreList4NumOfMatchesScore.size() == 1) {
            //By subtracting matchWeight from entity's total weight I bring the status
            //of totalWeight to before the above calculation process.
            for(Long entID : entityIDandScoreList4NumOfMatchesScore.keySet()) {
                this.getSearchResults2ge().get(entID).setTotalWeight((this.getSearchResults2ge().get(entID).getTotalWeight()-entityIDandScoreList4NumOfMatchesScore.get(entID)) + (entityIDandScoreList4NumOfMatchesScore.get(entID) * dct.distanceCalcSettingsList.get(0).getNumOfMatchesWeightA()));
            }
        //} else if (entityIDList4NumOfMatchesScore.size() > 1 && entityIDList4NumOfMatchesScore.size() < 5) {
          } else if (entityIDandScoreList4NumOfMatchesScore.size() > 1 && entityIDandScoreList4NumOfMatchesScore.size() < 5) {
            for(Long entID : entityIDandScoreList4NumOfMatchesScore.keySet()) {
                //this.getSearchResults2ge().get(entID).setTotalWeight((this.getSearchResults2ge().get(entID).getTotalWeight()-matchWeight) + (matchWeight * dct.distanceCalcSettingsList.get(0).getNumOfMatchesWeightB()));
                this.getSearchResults2ge().get(entID).setTotalWeight((this.getSearchResults2ge().get(entID).getTotalWeight()-entityIDandScoreList4NumOfMatchesScore.get(entID)) + (entityIDandScoreList4NumOfMatchesScore.get(entID) * dct.distanceCalcSettingsList.get(0).getNumOfMatchesWeightB()));
            }
        } else if (entityIDandScoreList4NumOfMatchesScore.size() > 4 && entityIDandScoreList4NumOfMatchesScore.size() < 13) {
            for(Long entID : entityIDandScoreList4NumOfMatchesScore.keySet()) {
                this.getSearchResults2ge().get(entID).setTotalWeight((this.getSearchResults2ge().get(entID).getTotalWeight()-entityIDandScoreList4NumOfMatchesScore.get(entID)) + (entityIDandScoreList4NumOfMatchesScore.get(entID) * dct.distanceCalcSettingsList.get(0).getNumOfMatchesWeightC()));
            }
        } else {
            //do nothing for now
        }

    }

}
